import {createSlice} from '@reduxjs/toolkit'
import request from '@/utils/request'
import { setToken as _setToklen, getToken} from '@/utils'
const userStore= createSlice({
    name:'user',
    initialState:{
        // token:''
        token:getToken()||''
    },
    // 同步修改
    reducers:{
        setToken(state,action){
            state.token=action.payload
            //localstore存一份
            // localStorage.setItem('token_key',action.payload)//toke持久化
            _setToklen(action.payload)
        },
    }
})

// 解构出actionCreater
const {setToken} =userStore.actions


// 异步使用
const fetchLogin=(loginForm)=>{
    return async(dispath)=>{
        const res=await request('/authorizations','post',loginForm)
        // 提交数据
        dispath(setToken(res.data.token))
    }
}
export {fetchLogin,setToken}
// 获取reducerp配置
const userReducer=userStore.reducer
export default userReducer